package com.mall.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: 谢君臣
 * @Date: 2022/8/10-9:26
 * @version: 1.0
 * @function:
 */
@Configuration
public class FeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                // 用线程池异步调用会 获取不到 session
                HttpServletRequest request = requestAttributes.getRequest();
                // 同步请求头
                requestTemplate.header("Cookie",request.getHeader("Cookie"));
            }
        };
    }
}
